package cn.example.demo.common.validation.sequence;

import cn.example.demo.common.validation.groups.Cascade;
import cn.example.demo.modules.house.dto.HousingOrderDto;
import cn.hutool.core.util.StrUtil;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Lizuxian
 * @create 2022-04-22 17:35
 */
@Component
public class HousingOrderSequenceProvider implements DefaultGroupSequenceProvider<HousingOrderDto> {
    @Override
    public List<Class<?>> getValidationGroups(HousingOrderDto object) {
        List<Class<?>> defaultGroupSequence = new ArrayList<>();
        // 添加要检验的参数结构体。这一步不能省,否则Default分组都不会执行了，会抛错的
        defaultGroupSequence.add(HousingOrderDto.class);
        // 这块判空请务必要做
        if (object != null) {
            // 若该字段为 true，则增加分组校验标记 @Cascade
            if ("1".equals(object.getIsNeedMeal())) {
                defaultGroupSequence.add(Cascade.class);
            }
            // TODO-lzx 控制器默认的分组校验不会覆盖，所以以下这段不需要追加到分组序列
//            defaultGroupSequence.add(Insert.class);
        }

        return defaultGroupSequence;
    }

    /**
     * 可调用此方法共享变量
     *
     * @param val
     */
    void shareVar(String val) {
        HttpServletRequest httpServletRequest =
                ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

//        httpServletRequest.setAttribute("var", val);
    }
}
